/**
 * 
 */
package com.baihui.taobao.db;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.baihui.taobao.util.Util;
import com.baihui.taobao.vo.BItemCat;
import com.taobao.api.TaobaoObject;
import com.taobao.api.domain.ItemCat;

/**
 * @author dev
 *
 */
public class ItemCatDAO extends GeneDAO {

	@Override
	protected String getPrimaryKey(Class aClass) {

		return "cid";
	}

	@Override
	protected Class getDomainClass() {
	
		return BItemCat.class;
	}

	@Override
	protected String parseRelationType(Field field) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Object parseValue(Object value, Field field) {
		// TODO Auto-generated method stub
		return value;
	}

	/** (non-Javadoc)
	 * @see com.baihui.taobao.db.GeneDAO#insert(java.util.List, java.sql.Connection)
	 */
	@Override
	public void insert(List<TaobaoObject> infos, Connection cn)
			throws Exception {
	       if (infos == null || infos.size() <= 0) return;
	       long parentID=-1;
	        List<Object[]> values = new ArrayList<Object[]>();
	        for (Object taobaoObject : infos) {
	            List<Object> rowValues = new ArrayList<Object>();
	            
	            ItemCat itemCat=(ItemCat)taobaoObject;
	            
	            if(parentID==-1){
	            	parentID=itemCat.getParentCid();
	            }
	            
	            rowValues.add(itemCat.getCid());
	            rowValues.add(itemCat.getParentCid());
	            String[] levelCaption=itemCat.getName().split("--");
	            for(int nIndex=0;nIndex<5;nIndex++) {
	            	if(nIndex<levelCaption.length) {
	            		rowValues.add(levelCaption[nIndex]);
	            	}
	            	else{
	            		rowValues.add(null);
	            	}
	            }
	            
	            rowValues.add(itemCat.getIsParent());
	            rowValues.add(itemCat.getStatus());
	            rowValues.add(itemCat.getSortOrder());
	            values.add(rowValues.toArray());
	        }
	        
	        deleteItemCat(cn,parentID);
	        updateAndInsert(cn,"insert into titemcat(cid,parentcid,name_1,name_2,name_3,name_4,name_5,isparent,status,sortorder) values(?,?,?,?,?,?,?,?,?,?)",values);
	}

	/**
     * 删除指定类目下的所有子类目
     */
    public void deleteItemCat(Connection cn,long parentID) throws Exception {
         java.sql.PreparedStatement ps = null;
         ResultSet rs = null;
         try {
        	 cn=DBConnectionMgr.getConnection();
             ps = cn.prepareStatement("delete from titemcat where parentcid=?");
             ps.setLong(1, parentID);
             ps.execute();
         } catch (Exception e) {
        	 e.printStackTrace();
             throw e;
         } finally {
             releaseConnection(cn, ps, rs);
         }
    }
    
}
